package me.flyray.merchant.serverDemo.severImpl;

import android.content.BroadcastReceiver;
import android.content.ContentProvider;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import me.flyray.merchant.serverDemo.okHttpUtil.OKHttpUtil;
import me.flyray.merchant.serverDemo.severIntereface.SeverInterFace;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/19 0019.
 */

public class SeverImpl implements SeverInterFace {
    private OKHttpUtil okHttpUtil = new OKHttpUtil();
    private Gson gson = new Gson();
    BroadcastReceiver broadcastReceiver;//广播接收者
    ContentProvider contentProvider;// 内容提供者

    private Context context;

    public SeverImpl(Context context) {
        this.context = context;
    }

/*
检测当前网络状态的  true:有
 */
    public boolean isNetworkConnected() {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    @Override
    public String login(String merName, String pwd) {

        if(!isNetworkConnected()){
            return "无网络";
        }
        String reqJrnNo = "11111111";
        String response = "";
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> mapSign = new HashMap<String, Object>();

        mapSign.put("password", pwd);
        mapSign.put("counterNo", merName);
        mapSign.put("version", "1.0");
        mapSign.put("txCode", "appLoginProcess");
        mapSign.put("reqJrnNo", reqJrnNo);
//        mapSign.put("timestamp", creatTime());
        mapSign.put("terminalType", "01");

        map.put("version", "1.0");
        map.put("txCode", "appLoginProcess");
        map.put("reqJrnNo", reqJrnNo);
//        map.put("timestamp", creatTime());
//        map.put("sign", creatSign(merName, mapSign, 0));
        map.put("counterNo", merName);
        map.put("password", pwd);
        map.put("terminalType", "01");
        String json = gson.toJson(map);
        try {
//            response = okHttpUtil.postDataString("http://114.215.45.205:8107/merchant/appLoginProcess.api", json);
//            https://mer.icloudpay.me:2900/merchant/firstLogin.do
            response = okHttpUtil.postDataString("https://mer.icloudpay.me:2900/merchant/appLoginProcess.api", json);
            Log.i("sd",response);
        } catch (IOException e) {
            response = "网络连接异常";
        }
        return response;
    }
}
